<?php

    require('_header.php');

    function contact_update($contact, $db) {
        // UPDATE  `contacts` SET  `prefix` =  'ms',
        // `first` =  'Janine',
        // `last` =  'Rice',
        // `major` =  'Math',
        // `email` =  'adsf@gmail.com' WHERE  `contacts`.`id` =1;
        $str = '';
        foreach($contact as $key => $value) {
            // if $key != 'id'
            if (strcmp($key, 'id')) {
                $str .= $key."='".$value."',";
            }
        }
        // delete last comma
        $str = substr($str, 0, -1);
        $db->get_results("UPDATE contacts SET $str WHERE id=". $contact['id']);
    }
    
    function response($contact, $success = true, $msg = '') {
        $arr = array('success' => $success, '$msg' => $msg, 'contact' => $contact);
        echo json_encode($arr);
    }
        
    // get $contact from $_POST
    $contact = $_POST['contact'];
    contact_update($contact, $db);
    response($contact);

?>